﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using Telerik.Web.Mvc;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Views.ViewModels;
using IndoReputation.Commons;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class MediaCategoryController : BaseController
    {
        //
        // GET: /MediaCategory/

        public ActionResult Index()
        {
            ViewData["ajax"] = true;
            ViewData["scrolling"] = true;
            ViewData["paging"] = true;
            ViewData["filtering"] = true;
            ViewData["grouping"] = true;
            ViewData["sorting"] = true;
            ViewData["showFooter"] = true;
            //return View(MediaCategoryBOL.GetAllMediaCategory());            
            return View();
        }

        [GridAction]
        public ActionResult _Index()
        {
            return View(new GridModel(MediaCategoryBOL.GetAllMediaCategory()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record
                if (!MediaCategoryBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"This Media Category have media. Please check again!");
                }
            }
            //Rebind the grid
            return View(new GridModel(MediaCategoryBOL.GetAllMediaCategory()));
        }

        [HttpGet]
        public ActionResult Add()
        {            
            SelectList sl = CommonFunctions.ToSelectList<MediaCategorySection>(new MediaCategorySection());
            var model = new MediaCategoryViewModel();
            model.MediaCategoryListSection = sl;
            return View(model);            
        }

        [HttpPost]
        public ActionResult Add(MediaCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                var mediaCategory = new MediaCategory();
                MediaCategorySection x = CommonFunctions.StringToEnum<MediaCategorySection>(model.MediaCategorySectionString);
                switch (x)
                {
                    case MediaCategorySection.Newspaper:
                        mediaCategory.IsNewspaper = true;
                        mediaCategory.IsOnlineNews = false;
                        mediaCategory.IsRadio = false;
                        mediaCategory.IsTV = false;
                        break;
                    case MediaCategorySection.OnlineNews:
                        mediaCategory.IsNewspaper = false;
                        mediaCategory.IsOnlineNews = true;
                        mediaCategory.IsRadio = false;
                        mediaCategory.IsTV = false;
                        break;
                    case MediaCategorySection.Radio:
                        mediaCategory.IsNewspaper = false;
                        mediaCategory.IsOnlineNews = false;
                        mediaCategory.IsRadio = true;
                        mediaCategory.IsTV = false;
                        break;
                    case MediaCategorySection.Television:
                        mediaCategory.IsNewspaper = false;
                        mediaCategory.IsOnlineNews = false;
                        mediaCategory.IsRadio = false;
                        mediaCategory.IsTV = true;
                        break;
                    default:
                        break;
                }
                mediaCategory.Description = model.Description;
                mediaCategory.IsOnline = model.IsOnline;
                mediaCategory.Name = model.Name.Trim();
                MediaCategoryBOL.Insert(mediaCategory);
            }
            //return View(model);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int mediaCategoryID)
        {
            SelectList sl = CommonFunctions.ToSelectList<MediaCategorySection>(new MediaCategorySection());
            var mediaCategory = MediaCategoryBOL.GetMediaCategoryById(mediaCategoryID);
            var model = new MediaCategoryViewModel();
            model.MediaCategoryListSection = sl;
            if (mediaCategory.IsNewspaper)
                model.MediaCategorySectionString = MediaCategorySection.Newspaper.ToString();
            else if (mediaCategory.IsOnlineNews)
                model.MediaCategorySectionString = MediaCategorySection.OnlineNews.ToString();
            else if (mediaCategory.IsRadio)
                model.MediaCategorySectionString = MediaCategorySection.Radio.ToString();
            else if (mediaCategory.IsTV)
                model.MediaCategorySectionString = MediaCategorySection.Television.ToString();
            model.CreateAt = mediaCategory.CreateAt;
            model.CreateBy = mediaCategory.CreateBy;
            model.Description = mediaCategory.Description.Trim();
            model.ID = mediaCategory.ID;
            model.IsOnline = mediaCategory.IsOnline;
            model.Name = mediaCategory.Name.Trim();
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(MediaCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                var mediaCategory = new MediaCategory();
                mediaCategory.ID = model.ID;
                mediaCategory.CreateAt = model.CreateAt;
                mediaCategory.CreateBy = model.CreateBy;
                mediaCategory.Description = model.Description;
                mediaCategory.IsOnline = model.IsOnline;
                mediaCategory.Name = model.Name.Trim();
                MediaCategorySection x = CommonFunctions.StringToEnum<MediaCategorySection>(model.MediaCategorySectionString);
                switch (x)
                {
                    case MediaCategorySection.Newspaper:
                        mediaCategory.IsNewspaper = true;
                        mediaCategory.IsOnlineNews = false;
                        mediaCategory.IsRadio = false;
                        mediaCategory.IsTV = false;
                        break;
                    case MediaCategorySection.OnlineNews:
                        mediaCategory.IsNewspaper = false;
                        mediaCategory.IsOnlineNews = true;
                        mediaCategory.IsRadio = false;
                        mediaCategory.IsTV = false;
                        break;
                    case MediaCategorySection.Radio:
                        mediaCategory.IsNewspaper = false;
                        mediaCategory.IsOnlineNews = false;
                        mediaCategory.IsRadio = true;
                        mediaCategory.IsTV = false;
                        break;
                    case MediaCategorySection.Television:
                        mediaCategory.IsNewspaper = false;
                        mediaCategory.IsOnlineNews = false;
                        mediaCategory.IsRadio = false;
                        mediaCategory.IsTV = true;
                        break;
                    default:
                        break;
                }                
                MediaCategoryBOL.Update(mediaCategory);
            }            
            return RedirectToAction("Index");
        }
    }
}
